package com.ruoyi.community.controller.wxApi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.Exercise;
import com.ruoyi.community.service.IExerciseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 活动Controller
 *
 * @author lj
 * @date 2022-02-25
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/wx/community/exercise")
@Api(tags = {"活动"})
public class WxExerciseController extends BaseController {
    @Autowired
    private IExerciseService exerciseService;

    /**
     * 查询活动列表
     */
    @ApiOperation(value = "查询活动列表")
    @GetMapping("/list")
    public TableDataInfo list(Exercise exercise) {
        startPage();
        List<Exercise> list = exerciseService.selectExerciseList(exercise);
        if (ObjectUtils.isEmpty(list)){
            return getNoDataTable(list);
        }
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @ApiOperation(value = "导出活动列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Exercise exercise) {
        List<Exercise> list = exerciseService.selectExerciseList(exercise);
        ExcelUtil<Exercise> util = new ExcelUtil<Exercise>(Exercise.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @ApiOperation(value = "获取活动详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(exerciseService.selectExerciseById(id));
    }

    /**
     * 新增活动
     */
    @ApiOperation(value = "新增活动")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Exercise exercise) {
        return toAjax(exerciseService.insertExercise(exercise));
    }

    /**
     * 修改活动
     */
    @ApiOperation(value = "修改活动")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Exercise exercise) {
        return toAjax(exerciseService.updateExercise(exercise));
    }

    /**
     * 删除活动
     */
    @ApiOperation(value = "删除活动")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        Long[] ids= {id};
        return toAjax(exerciseService.updateExerciseByIds(ids));
    }

    @ApiOperation(value = "删除活动1")
    @PostMapping("/remove1")
    public AjaxResult remove1(Long[] ids)
    {
        return toAjax(exerciseService.updateExerciseByIds(ids));
    }

    /**
     * 我的活动
     */
    @ApiOperation(value = "我的活动")
    @GetMapping(value = "/myExercise")
    public AjaxResult myExercise(Long userId) {
        return AjaxResult.success(exerciseService.myExercise(userId));
    }
}
